using System;
using System.Globalization;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.SupportingMaterials
{
    public partial class EditASupportingMaterials : Page
    {
        /// <summary>
        /// Gets or sets the organization id.
        /// </summary>
        /// <value>
        /// The organization id.
        /// </value>
        public int OrganizationId
        {
            get
            {
                if (ViewState["OrganizationId"] != null)
                {
                    return (int) ViewState["OrganizationId"];
                }
                return -1;
            }

            set { ViewState["OrganizationId"] = value; }
        }

        /// <summary>
        /// Gets or sets the supporting material id.
        /// </summary>
        /// <value>
        /// The supporting material id.
        /// </value>
        public int SupportingMaterialId
        {
            get
            {
                if (ViewState["SupportingMaterialId"] != null)
                {
                    return (int) ViewState["SupportingMaterialId"];
                }
                return -1;
            }

            set { ViewState["SupportingMaterialId"] = value; }
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                if (!String.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    string id = Request.QueryString["id"];
                    LoadAllData(int.Parse(id, CultureInfo.CurrentCulture));
                }
            }
        }

        /// <summary>
        /// Loads the data.
        /// </summary>
        private void LoadData()
        {
            DropDownListType.DataSource = DocumentUtilities.SupportingMaterialTypes;
            DropDownListType.DataBind();
        }

        /// <summary>
        /// Loads all data.
        /// </summary>
        /// <param name="id">The id.</param>
        private void LoadAllData(int id)
        {
            SupportingMaterial data = ASupportingMaterialBO.GetSupportingMaterialById(id);
            data.SupportingMaterialId = id;
            TextBoxURL.Text = data.Url;
            TextBoxAddedBy.Text = data.User.Account;
            TextBoxAddedDate.Text = String.Format(CultureInfo.CurrentCulture, "{0:dd/MM/yyyy}", data.AddedDate);
            TextBoxDescription.Text = data.Description;
            LabelIsActive.Text = data.IsActive ? "Yes" : "No";
            DropDownListType.Text = data.Type;
        }

        /// <summary>
        /// Handles the Command event of the ButtonSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void ButtonSave_Command(object sender, CommandEventArgs e)
        {
            var supportingMaterial = new SupportingMaterial();
            //var Organization=from DB.Con
            if (!String.IsNullOrEmpty(Request.QueryString["id"]))
            {
                supportingMaterial.SupportingMaterialId = int.Parse(Request.QueryString["id"],
                                                                    CultureInfo.CurrentCulture);
            }
            supportingMaterial.Url = String.IsNullOrEmpty(TextBoxURL.Text) ? String.Empty : TextBoxURL.Text;
            //supportingMaterial.User.Account = HttpContext.Current.User.Identity.Name;
            supportingMaterial.Description = TextBoxDescription.Text;
            supportingMaterial.Type = DropDownListType.Text;
            supportingMaterial.UserId = (int) Session["userId"];
            //supportingMaterial.IsActive= LabelIsActive.Text.Equals("Yes");
            bool updateSupportingMaterial = ASupportingMaterialBO.UpdateSupportingMaterial(supportingMaterial);
            if (updateSupportingMaterial)
            {
                int organisationId =
                    ASupportingMaterialBO.GetSupportingMaterialById(supportingMaterial.SupportingMaterialId).
                        OrganizationId;
                Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                                "~/Organisation/OrganisationDetails02.aspx?id={0}#DetailsPanel05",
                                                organisationId));
            }
            else
            {
                Response.Write("<script>alert('Update To Database Failed!')</script>");
            }
        }

        /// <summary>
        /// Handles the Command event of the ButtonInactive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void ButtonInactive_Command(object sender, CommandEventArgs e)
        {
            ASupportingMaterialBO.SetSupportingMaterialStatus(SupportingMaterialId, false);
            ButtonInactive.Visible = false;
        }

        /// <summary>
        /// Handles the Command event of the ButtonBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void ButtonBack_Command(object sender, CommandEventArgs e)
        {
            Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                            "~/Organisation/OrganisationDetails02.aspx?id={0}#DetailsPanel05",
                                            OrganizationId));
        }
    }
}